package cn.edu.nuc.ssm.controller.film;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.edu.nuc.ssm.controller.page.Pager;
import cn.edu.nuc.ssm.domain.PlanFilm;
import cn.edu.nuc.ssm.service.interfaces.PlanFilmService;

@Controller
@RequestMapping("planfilm")
public class PlanFilmController {
    @Autowired
	private PlanFilmService service;
    
    /**
   	 * 通过servlet进入添加排片页面。
   	 * @return
   	 */
       @RequestMapping(value="add",method=RequestMethod.GET)
       public String getAdd(){
       	return "film/plan/add";
       }
       /**
        * 添加排片（post）
        * @param plan
        * @return
        */
       @RequestMapping(value="add",method=RequestMethod.POST)
       public String addPlan(PlanFilm plan){
       	int result = service.insert(plan);
       	if(result == 1){
       		return "redirect:list";
       	}
       	return "error";
       } 
   	/**
   	 * 进入排片列表(查询)
   	 * @return
   	 */
   	@RequestMapping(value="list",method=RequestMethod.GET)
   	public String getPlan(@RequestParam(name="planfilmname",defaultValue="") String planfilmname,
   			@RequestParam(name="current",defaultValue="1") int current , 
   			Model model){
   		Pager<PlanFilm> pager = service.pageList(planfilmname, current);
   		model.addAttribute("pager", pager);
   		return "film/plan/list";
   	}
   	
   	/**
   	 * 更新，（全部进行更新）
   	 */
   	//获取排片信息
   	@RequestMapping(value="detail/{planid}",method=RequestMethod.GET)
   	public String updateplan(@PathVariable("planid") Integer planid,Model model){
   		PlanFilm plan = service.selectByPrimaryKey(planid);
   		model.addAttribute("plan", plan);
   		return "film/plan/update";
   	}
   	//更新
   	@RequestMapping(value="update/{planid}",method=RequestMethod.PUT)
   	public String updateplan(@PathVariable("planid") Integer planid,PlanFilm plan){
   		int result = service.updateByPrimaryKey(plan);
   		if(result == 1){
   			return "redirect:/planfilm/list";	
   		}
   		return  "redirect:error";
   	}
   	//删除单个排片信息。
   	@RequestMapping(value="delete/{planid}",method=RequestMethod.DELETE)
   	public String deleteFun(@PathVariable("planid") Integer planid){
   		int result = service.deleteByPrimaryKey(planid);
   		if(result==1){
   			return "redirect:/planfilm/list";	
   		}
   		return  "error";
   	}
}
